﻿/****************************************************************************************
*	Mouzetrap - client/server framework	
*
*	$HeadURL: http://mouzetrap.googlecode.com/svn/trunk/Mouzetrap/MathHelper.cs $
*	$Id: MathHelper.cs 11 2009-06-11 09:10:25Z tobz@fallenrealms.net $
* 
*	Copyright (c) 2008 Toby Lawrence
* 
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
* 
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
* 
****************************************************************************************/

namespace Mouzetrap
{
	/// <summary>
	/// Helper class for miscellaneous math functions.
	/// </summary>
	public static class MathHelper
	{
		/// <summary>
		/// Calculates the next highest power of two.
		/// </summary>
		/// <param name="number">the number to search past</param>
		/// <returns>the closest power of two, but not less than <paramref name="number" /></returns>
		public static short GetNextPowerOfTwo(short number)
		{
			return (short) GetNextPowerOfTwo((int) number);
		}

		/// <summary>
		/// Calculates the next highest power of two.
		/// </summary>
		/// <param name="number">the number to search past</param>
		/// <returns>the closest power of two, but not less than <paramref name="number" /></returns>
		public static int GetNextPowerOfTwo(int number)
		{
			int roundedNumber = number;

			// begin power of two calculation
			roundedNumber--;
			roundedNumber = roundedNumber | roundedNumber >> 1;
			roundedNumber = roundedNumber | roundedNumber >> 2;
			roundedNumber = roundedNumber | roundedNumber >> 4;
			roundedNumber = roundedNumber | roundedNumber >> 8;
			roundedNumber = roundedNumber | roundedNumber >> 16;
			roundedNumber++;

			return roundedNumber;
		}

		/// <summary>
		/// Calculates the next highest power of two.
		/// </summary>
		/// <param name="number">the number to search past</param>
		/// <returns>the closest power of two, but not less than <paramref name="number" /></returns>
		public static long GetNextPowerOfTwo(long number)
		{
			long roundedNumber = number;

			// begin power of two calculation
			roundedNumber--;
			roundedNumber = roundedNumber | roundedNumber >> 1;
			roundedNumber = roundedNumber | roundedNumber >> 2;
			roundedNumber = roundedNumber | roundedNumber >> 4;
			roundedNumber = roundedNumber | roundedNumber >> 8;
			roundedNumber = roundedNumber | roundedNumber >> 16;
			roundedNumber = roundedNumber | roundedNumber >> 32;
			roundedNumber++;

			return roundedNumber;
		}

		/// <summary>
		/// Calculates the closest highest power of two.
		/// </summary>
		/// <param name="number">the number to search around</param>
		/// <returns>the number that is the closest power of two</returns>
		public static short GetNearestPowerOfTwo(short number)
		{
			return (short) GetNearestPowerOfTwo((int) number);
		}

		/// <summary>
		/// Calculates the closest highest power of two.
		/// </summary>
		/// <param name="number">the number to search around</param>
		/// <returns>the number that is the closest power of two</returns>
		public static int GetNearestPowerOfTwo(int number)
		{
			int roundedNumber = number;

			// begin power of two calculation
			roundedNumber--;
			roundedNumber = roundedNumber | roundedNumber >> 1;
			roundedNumber = roundedNumber | roundedNumber >> 2;
			roundedNumber = roundedNumber | roundedNumber >> 4;
			roundedNumber = roundedNumber | roundedNumber >> 8;
			roundedNumber = roundedNumber | roundedNumber >> 16;
			roundedNumber++;

			// get the next lowest power of two if we're closer to it, since this algorithm
			// always returns the next highest, and we may not want to go that high.
			if((roundedNumber - number) > (number - (roundedNumber / 2)))
			{
				// it's closer to the previous power, so divide by two.
				roundedNumber /= 2;
			}

			return roundedNumber;
		}

		/// <summary>
		/// Calculates the closest highest power of two.
		/// </summary>
		/// <param name="number">the number to search around</param>
		/// <returns>the number that is the closest power of two</returns>
		public static long GetNearestPowerOfTwo(long number)
		{
			long roundedNumber = number;

			// begin power of two calculation
			roundedNumber--;
			roundedNumber = roundedNumber | roundedNumber >> 1;
			roundedNumber = roundedNumber | roundedNumber >> 2;
			roundedNumber = roundedNumber | roundedNumber >> 4;
			roundedNumber = roundedNumber | roundedNumber >> 8;
			roundedNumber = roundedNumber | roundedNumber >> 16;
			roundedNumber = roundedNumber | roundedNumber >> 32;
			roundedNumber++;

			// get the next lowest power of two if we're closer to it, since this algorithm
			// always returns the next highest, and we may not want to go that high.
			if((roundedNumber - number) > (number - (roundedNumber / 2)))
			{
				// it's closer to the previous power, so divide by two.
				roundedNumber /= 2;
			}

			return roundedNumber;
		}

		/// <summary>
		/// Determines if a number is a power of two.
		/// </summary>
		/// <param name="value">the number to check</param>
		/// <returns>true if the number is a power of two; false otherwise</returns>
		public static bool IsPowerOfTwo(short value)
		{
			return value != 0 ? ((value & -value) == value) : false;
		}

		/// <summary>
		/// Determines if a number is a power of two.
		/// </summary>
		/// <param name="value">the number to check</param>
		/// <returns>true if the number is a power of two; false otherwise</returns>
		public static bool IsPowerOfTwo(int value)
		{
			return value != 0 ? ((value & -value) == value) : false;
		}

		/// <summary>
		/// Determines if a number is a power of two.
		/// </summary>
		/// <param name="value">the number to check</param>
		/// <returns>true if the number is a power of two; false otherwise</returns>
		public static bool IsPowerOfTwo(long value)
		{
			return value != 0 ? ((value & -value) == value) : false;
		}
	}
}